// pages/setting/changePwd.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userPassword: '',
    userPasswordAgain: '',
    repetition: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 密码
  userPasswordInput: function (e) {
    this.setData({
      userPassword: e.detail.value
    });
  },
  // 再次输入密码
  userPasswordAgainInput: function (e) {
    this.setData({
      userPasswordAgain: e.detail.value
    });
  },
  update: function () {
    const db = wx.cloud.database()
    db.collection('userInfo').get().then(res => {

      if (res.data.length == 1) {
        // res.data 包含该记录的数据
        var id = res.data[0]._id
        var password = res.data[0].userPassword
        var userPassword = this.data.userPassword;
        var userPasswordAgain = this.data.userPasswordAgain;
        if (userPassword === password) {
          wx.showToast({
            title: '新旧密码不能一样',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        } else if (userPassword.length < 6) {
          wx.showToast({
            title: '密码最少6位',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        } else if (userPassword !== userPasswordAgain) {
          wx.showToast({
            title: '两次密码输入不一致',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        } else {
          db.collection('userInfo').doc(id).update({
            data: {
              userPassword: this.data.userPassword
            },
            success: function (res) {
              wx.showToast({
                title: '修改成功',
                duration: 2000,
                icon: 'success',
                mask: true
              })
              wx.switchTab({
                url: '../mine/mine',
              })
              console.log(res);
            }
          })
        }
      } else {
        wx.showToast({
          title: '未注册',
        })
      }

    })
  }
})